package com.xjrsoft.module.customer.erp_model.eRP_FinanceExpenditure.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.page.ConventPage;
import java.util.List;
import com.xjrsoft.common.utils.DataTransUtil;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceExpenditure.dto.ErpFinanceexpenditureDto;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceExpenditure.dto.SaveErpFinanceexpenditureFormDataDto;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceExpenditure.vo.ErpFinanceexpenditureFormDataVo;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceExpenditure.vo.ErpFinanceexpenditureListVo;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceExpenditure.dto.ErpFinanceexpenditureListDto;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceExpenditure.vo.ErpFinanceexpenditurebillVo;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceExpenditure.entity.ErpFinanceexpenditurebill;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceExpenditure.dto.ErpFinanceexpenditurebillDto;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.FlowableUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import com.xjrsoft.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceExpenditure.entity.ErpFinanceexpenditure;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceExpenditure.vo.ErpFinanceexpenditureVo;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceExpenditure.service.IErpFinanceexpenditureService;

/**
 *  控制器
 *
 * @author pp
 * @since 2021-09-23
 */
@RestController
@AllArgsConstructor
@RequestMapping("/erp_financeexpenditure")
@Api(value = "其他支出", tags = "其他支出接口")
public class ErpFinanceexpenditureController {


	private final IErpFinanceexpenditureService erpFinanceexpenditureService;


	/**
	 * 详情
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "详情", notes = "传入erpFinanceexpenditure")
	public Response<ErpFinanceexpenditureFormDataVo> getFormData(@PathVariable String id) {
		ErpFinanceexpenditureFormDataVo formDataVo = new ErpFinanceexpenditureFormDataVo();
		// 主表数据
		ErpFinanceexpenditure erpFinanceexpenditure = erpFinanceexpenditureService.getById(id);
		formDataVo.setErpFinanceexpenditureVo(BeanUtil.copy(erpFinanceexpenditure, ErpFinanceexpenditureVo.class));
		// 从表数据
		formDataVo.setErpFinanceexpenditurebillVoList(BeanUtil.copyList(erpFinanceexpenditureService.getErpFinanceexpenditurebillByParentId(id), ErpFinanceexpenditurebillVo.class));
		return Response.ok(formDataVo);
	}

	/**
	 * 自定义分页 
	 */
	@GetMapping
	@ApiOperation(value = "分页", notes = "传入erpFinanceexpenditure")
	public Response<PageOutput<ErpFinanceexpenditureListVo>> getPageList(ErpFinanceexpenditureListDto listDto) {
		IPage<ErpFinanceexpenditure> page = erpFinanceexpenditureService.getPageList(listDto);
		List<ErpFinanceexpenditureListVo> records = BeanUtil.copyList(page.getRecords(), ErpFinanceexpenditureListVo.class);
		// 转换列表数据
		DataTransUtil.transListShowData(records);
		// 工作流数据处理
		FlowableUtil.systemFormList(records, SecureUtil.getModuleId());
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
	}

	/**
	 * 新增 
	 */
	@PostMapping
	@ApiOperation(value = "新增", notes = "传入erpFinanceexpenditure")
	public Response save(@RequestBody SaveErpFinanceexpenditureFormDataDto formDto) {
		ErpFinanceexpenditureDto erpFinanceexpenditureDto = formDto.getErpFinanceexpenditureDto();
		ErpFinanceexpenditure erpFinanceexpenditure = BeanUtil.copy(erpFinanceexpenditureDto, ErpFinanceexpenditure.class);
		List<ErpFinanceexpenditurebillDto> erpFinanceexpenditurebillDtoList = formDto.getErpFinanceexpenditurebillDto();
		List<ErpFinanceexpenditurebill> erpFinanceexpenditurebillList = BeanUtil.copyList(erpFinanceexpenditurebillDtoList, ErpFinanceexpenditurebill.class);
		String dataId = erpFinanceexpenditureService.addErpFinanceexpenditure(erpFinanceexpenditure, erpFinanceexpenditurebillList);
		return dataId == null ? Response.notOk() : Response.ok(dataId);
	}

	/**
	 * 修改 
	 */
	@PutMapping("/{id}")
	@ApiOperation(value = "修改", notes = "传入erpFinanceexpenditure")
	public Response update(@PathVariable String id, @RequestBody SaveErpFinanceexpenditureFormDataDto formDto) {
		ErpFinanceexpenditureDto erpFinanceexpenditureDto = formDto.getErpFinanceexpenditureDto();
		ErpFinanceexpenditure erpFinanceexpenditure = BeanUtil.copy(erpFinanceexpenditureDto, ErpFinanceexpenditure.class);

		List<ErpFinanceexpenditurebillDto> erpFinanceexpenditurebillDtoList = formDto.getErpFinanceexpenditurebillDto();
		List<ErpFinanceexpenditurebill> erpFinanceexpenditurebillList = BeanUtil.copyList(erpFinanceexpenditurebillDtoList, ErpFinanceexpenditurebill.class);
		return Response.status(erpFinanceexpenditureService.updateErpFinanceexpenditure(id, erpFinanceexpenditure, erpFinanceexpenditurebillList));
	}


	
	/**
	 * 删除 
	 */
	@DeleteMapping("/{ids}")
	@ApiOperation(value = "删除", notes = "传入ids")
	public Response remove(@ApiParam(value = "主键集合", required = true) @PathVariable String ids) {
		return Response.status(erpFinanceexpenditureService.removeByIds(Func.toStrList(ids)));
	}

	
}
